*uncomment and run the 3 commands below 
*net from "http://www.nber.org/stata"
*net describe taxsim35
*net install taxsim35

set more off
global indata ".../data"


*------------------------------------------------------------------------------*
* Retired individuals (no freeze)
*------------------------------------------------------------------------------*

import delimited using "$indata/retinc_a_20.csv", clear //accum/decum choices with adaptive grid

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = 0      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0
gen pensions = .    //pension distributions
gen gssi = .        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pensions = v1
replace gssi = v2
replace page = v3
drop v1 v2 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fiitax page pensions gssi

*Export as csv for input into structural model
export delimited "$indata/retinc_taxsim_a_20.csv", novarnames replace

*------------------------------------------------------------------------------*
* Retired individuals (different freeze ages)
*------------------------------------------------------------------------------*

forvalues z = 7/15{
	import delimited using "$indata/retinc_a_20_z`z'.csv", clear //accum/decum choices with adaptive grid

	*file structure for taxsim

	gen taxsimid = _n   //_n
	gen year = 2010     //2010
	gen state  = 0     
	gen mstat = 1       //1 = single 
	gen page = .        //age
	gen sage = 0       
	gen depx = 0
	gen dep13 = 0
	gen dep17 = 0
	gen dep18 = 0
	gen pwages = 0      //annual labor earnings
	gen swages = 0
	gen dividends = 0
	gen intrec = 0
	gen stcg = 0 
	gen ltcg = 0
	gen otherprop = 0
	gen nonprop = 0
	gen pensions = .    //pension distributions
	gen gssi = .        //SS benefits
	gen ui = 0
	gen transfers = 0
	gen rentpaid = 0
	gen proptax = 0
	gen otheritem = 0
	gen childcare = 0
	gen mortgage = 0

	replace pensions = v1
	replace gssi = v2
	replace page = v3
	drop v1 v2 v3

	*Run the tax calculator
	cd "$indata"
	taxsim35, replace

	*Keep relevant vars
	keep fiitax page pensions gssi

	*Export as csv for input into structural model
	export delimited "$indata/retinc_taxsim_a_20_z`z'.csv", novarnames replace
}

*------------------------------------------------------------------------------*
* Retired individuals (different benefit cut ages)
*------------------------------------------------------------------------------*

forvalues z = 7/15{
	import delimited using "$indata/retinc_a_20_bcut`z'.csv", clear //accum/decum choices with adaptive grid

	*file structure for taxsim

	gen taxsimid = _n   //_n
	gen year = 2010     //2010
	gen state  = 0     
	gen mstat = 1       //1 = single 
	gen page = .        //age
	gen sage = 0       
	gen depx = 0
	gen dep13 = 0
	gen dep17 = 0
	gen dep18 = 0
	gen pwages = 0      //annual labor earnings
	gen swages = 0
	gen dividends = 0
	gen intrec = 0
	gen stcg = 0 
	gen ltcg = 0
	gen otherprop = 0
	gen nonprop = 0
	gen pensions = .    //pension distributions
	gen gssi = .        //SS benefits
	gen ui = 0
	gen transfers = 0
	gen rentpaid = 0
	gen proptax = 0
	gen otheritem = 0
	gen childcare = 0
	gen mortgage = 0

	replace pensions = v1
	replace gssi = v2
	replace page = v3
	drop v1 v2 v3

	*Run the tax calculator
	cd "$indata"
	taxsim35, replace

	*Keep relevant vars
	keep fiitax page pensions gssi

	*Export as csv for input into structural model
	export delimited "$indata/retinc_taxsim_a_20_bcut`z'.csv", novarnames replace
}

*------------------------------------------------------------------------------*
* Working individuals
*------------------------------------------------------------------------------*


*1. FIITAX (DC contrib deductable)
import delimited using "$indata/workinc_a_20.csv", clear //accum/decum choices with adaptive grid

*earnings after deducting DC contrib
gen dc_contrib = v2
gen earn_after_dc = v1-v2

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = earn_after_dc
replace page = v3

drop v1 v2 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fiitax page dc_contrib pwages
rename pwages earn_after_dc
gen id = _n
save "$indata/workinc_a_fiitax", replace

*1. FICA (DC contrib not deductable)
import delimited using "$indata/workinc_a_20.csv", clear

keep v1 v3

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = v1
replace page = v3

drop v1 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fica pwages 
rename pwages earn_before_dc
gen id = _n
merge 1:1 id using "$indata/workinc_a_fiitax"
drop _merge id
order page earn_before_dc fica earn_after_dc fiitax dc_contrib

*Export as csv for input into structural model
export delimited "$indata/workinc_taxsim_a_20.csv", novarnames replace

*------------------------------------------------------------------------------*
* Working individuals: freezes at different ages (average DC plan)
*------------------------------------------------------------------------------*

forvalues z = 7/15{

	*1. FIITAX (DC contrib deductable)
	import delimited using "$indata/workinc_a_20_z`z'.csv", clear //accum/decum choices with adaptive grid

	*earnings after deducting DC contrib
	gen dc_contrib = v2
	gen earn_after_dc = v1-v2

	*file structure for taxsim

	gen taxsimid = _n   //_n
	gen year = 2010     //2010
	gen state  = 0     
	gen mstat = 1       //1 = single 
	gen page = .        //age
	gen sage = 0       
	gen depx = 0
	gen dep13 = 0
	gen dep17 = 0
	gen dep18 = 0
	gen pwages = .      //annual labor earnings
	gen swages = 0
	gen dividends = 0
	gen intrec = 0
	gen stcg = 0 
	gen ltcg = 0
	gen otherprop = 0
	gen nonprop = 0     
	gen pensions = 0    //pension distributions
	gen gssi = 0        //SS benefits
	gen ui = 0
	gen transfers = 0
	gen rentpaid = 0
	gen proptax = 0
	gen otheritem = 0
	gen childcare = 0
	gen mortgage = 0

	replace pwages = earn_after_dc
	replace page = v3

	drop v1 v2 v3

	*Run the tax calculator
	cd "$indata"
	taxsim35, replace

	*Keep relevant vars
	keep fiitax page dc_contrib pwages
	rename pwages earn_after_dc
	gen id = _n
	save "$indata/workinc_a_fiitax_z`z'", replace

	*1. FICA (DC contrib not deductable)
	import delimited using "$indata/workinc_a_20_z`z'.csv", clear

	keep v1 v3

	*file structure for taxsim

	gen taxsimid = _n   //_n
	gen year = 2010     //2010
	gen state  = 0     
	gen mstat = 1       //1 = single 
	gen page = .        //age
	gen sage = 0       
	gen depx = 0
	gen dep13 = 0
	gen dep17 = 0
	gen dep18 = 0
	gen pwages = .      //annual labor earnings
	gen swages = 0
	gen dividends = 0
	gen intrec = 0
	gen stcg = 0 
	gen ltcg = 0
	gen otherprop = 0
	gen nonprop = 0     
	gen pensions = 0    //pension distributions
	gen gssi = 0        //SS benefits
	gen ui = 0
	gen transfers = 0
	gen rentpaid = 0
	gen proptax = 0
	gen otheritem = 0
	gen childcare = 0
	gen mortgage = 0

	replace pwages = v1
	replace page = v3

	drop v1 v3

	*Run the tax calculator
	cd "$indata"
	taxsim35, replace

	*Keep relevant vars
	keep fica pwages 
	rename pwages earn_before_dc
	gen id = _n
	merge 1:1 id using "$indata/workinc_a_fiitax_z`z'"
	drop _merge id
	order page earn_before_dc fica earn_after_dc fiitax dc_contrib

	*Export as csv for input into structural model
	export delimited "$indata/workinc_taxsim_a_20_z`z'.csv", novarnames replace


}

*------------------------------------------------------------------------------*
* Working individuals: freezes at different ages and different replacement options
*------------------------------------------------------------------------------*

local alt "cont" //tsp h1dc h2dc 
forvalues z = 7/15{

	foreach v of local alt{

		*1. FIITAX (DC contrib deductable)
		import delimited using "$indata/workinc_a_20_`v'z`z'.csv", clear //accum/decum choices with adaptive grid

		*earnings after deducting DC contrib
		gen dc_contrib = v2
		gen earn_after_dc = v1-v2

		*file structure for taxsim

		gen taxsimid = _n   //_n
		gen year = 2010     //2010
		gen state  = 0     
		gen mstat = 1       //1 = single 
		gen page = .        //age
		gen sage = 0       
		gen depx = 0
		gen dep13 = 0
		gen dep17 = 0
		gen dep18 = 0
		gen pwages = .      //annual labor earnings
		gen swages = 0
		gen dividends = 0
		gen intrec = 0
		gen stcg = 0 
		gen ltcg = 0
		gen otherprop = 0
		gen nonprop = 0     
		gen pensions = 0    //pension distributions
		gen gssi = 0        //SS benefits
		gen ui = 0
		gen transfers = 0
		gen rentpaid = 0
		gen proptax = 0
		gen otheritem = 0
		gen childcare = 0
		gen mortgage = 0

		replace pwages = earn_after_dc
		replace page = v3

		drop v1 v2 v3

		*Run the tax calculator
		cd "$indata"
		taxsim35, replace

		*Keep relevant vars
		keep fiitax page dc_contrib pwages
		rename pwages earn_after_dc
		gen id = _n
		save "$indata/workinc_a_fiitax_`v'z`z'", replace

		*1. FICA (DC contrib not deductable)
		import delimited using "$indata/workinc_a_20_`v'z`z'.csv", clear

		keep v1 v3

		*file structure for taxsim

		gen taxsimid = _n   //_n
		gen year = 2010     //2010
		gen state  = 0     
		gen mstat = 1       //1 = single 
		gen page = .        //age
		gen sage = 0       
		gen depx = 0
		gen dep13 = 0
		gen dep17 = 0
		gen dep18 = 0
		gen pwages = .      //annual labor earnings
		gen swages = 0
		gen dividends = 0
		gen intrec = 0
		gen stcg = 0 
		gen ltcg = 0
		gen otherprop = 0
		gen nonprop = 0     
		gen pensions = 0    //pension distributions
		gen gssi = 0        //SS benefits
		gen ui = 0
		gen transfers = 0
		gen rentpaid = 0
		gen proptax = 0
		gen otheritem = 0
		gen childcare = 0
		gen mortgage = 0

		replace pwages = v1
		replace page = v3

		drop v1 v3

		*Run the tax calculator
		cd "$indata"
		taxsim35, replace

		*Keep relevant vars
		keep fica pwages 
		rename pwages earn_before_dc
		gen id = _n
		merge 1:1 id using "$indata/workinc_a_fiitax_`v'z`z'"
		drop _merge id
		order page earn_before_dc fica earn_after_dc fiitax dc_contrib

		*Export as csv for input into structural model
		export delimited "$indata/workinc_taxsim_a_20_`v'z`z'.csv", novarnames replace

	}
}


*------------------------------------------------------------------------------*
* Working individuals 20 % earnings bump at 58
*------------------------------------------------------------------------------*

*1. FIITAX (DC contrib deductable)
import delimited using "$indata/workinc_s58_a_20.csv", clear

*earnings after deducting DC contrib
gen dc_contrib = v2
gen earn_after_dc = v1-v2

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = earn_after_dc
replace page = v3

drop v1 v2 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fiitax page dc_contrib pwages
rename pwages earn_after_dc
gen id = _n
save "$indata/workinc_s58_a_fiitax", replace

*1. FICA (DC contrib not deductable)
import delimited using "$indata/workinc_s58_a_20.csv", clear

keep v1 v3

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = v1
replace page = v3

drop v1 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fica pwages 
rename pwages earn_before_dc
gen id = _n
merge 1:1 id using "$indata/workinc_s58_a_fiitax"
drop _merge id
order page earn_before_dc fica earn_after_dc fiitax dc_contrib

*Export as csv for input into structural model
export delimited "$indata/workinc_taxsim_s58_a_20.csv", novarnames replace

*------------------------------------------------------------------------------*
* Working individuals 20 % earnings bump at 60
*------------------------------------------------------------------------------*

*1. FIITAX (DC contrib deductable)
import delimited using "$indata/workinc_s60_a_20.csv", clear

*earnings after deducting DC contrib
gen dc_contrib = v2
gen earn_after_dc = v1-v2

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = earn_after_dc
replace page = v3

drop v1 v2 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fiitax page dc_contrib pwages
rename pwages earn_after_dc
gen id = _n
save "$indata/workinc_s60_a_fiitax", replace

*1. FICA (DC contrib not deductable)
import delimited using "$indata/workinc_s60_a_20.csv", clear

keep v1 v3

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = v1
replace page = v3

drop v1 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fica pwages 
rename pwages earn_before_dc
gen id = _n
merge 1:1 id using "$indata/workinc_s60_a_fiitax"
drop _merge id
order page earn_before_dc fica earn_after_dc fiitax dc_contrib

*Export as csv for input into structural model
export delimited "$indata/workinc_taxsim_s60_a_20.csv", novarnames replace

*------------------------------------------------------------------------------*
* Working individuals 20 % earnings bump at 62
*------------------------------------------------------------------------------*

*1. FIITAX (DC contrib deductable)
import delimited using "$indata/workinc_s62_a_20.csv", clear

*earnings after deducting DC contrib
gen dc_contrib = v2
gen earn_after_dc = v1-v2

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = earn_after_dc
replace page = v3

drop v1 v2 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fiitax page dc_contrib pwages
rename pwages earn_after_dc
gen id = _n
save "$indata/workinc_s62_a_fiitax", replace

*1. FICA (DC contrib not deductable)
import delimited using "$indata/workinc_s62_a_20.csv", clear

keep v1 v3

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = v1
replace page = v3

drop v1 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fica pwages 
rename pwages earn_before_dc
gen id = _n
merge 1:1 id using "$indata/workinc_s62_a_fiitax"
drop _merge id
order page earn_before_dc fica earn_after_dc fiitax dc_contrib

*Export as csv for input into structural model
export delimited "$indata/workinc_taxsim_s62_a_20.csv", novarnames replace

*------------------------------------------------------------------------------*
* Working individuals 20 % earnings bump at 64
*------------------------------------------------------------------------------*

*1. FIITAX (DC contrib deductable)
import delimited using "$indata/workinc_s64_a_20.csv", clear

*earnings after deducting DC contrib
gen dc_contrib = v2
gen earn_after_dc = v1-v2

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = earn_after_dc
replace page = v3

drop v1 v2 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fiitax page dc_contrib pwages
rename pwages earn_after_dc
gen id = _n
save "$indata/workinc_s64_a_fiitax", replace

*1. FICA (DC contrib not deductable)
import delimited using "$indata/workinc_s64_a_20.csv", clear

keep v1 v3

*file structure for taxsim

gen taxsimid = _n   //_n
gen year = 2010     //2010
gen state  = 0     
gen mstat = 1       //1 = single 
gen page = .        //age
gen sage = 0       
gen depx = 0
gen dep13 = 0
gen dep17 = 0
gen dep18 = 0
gen pwages = .      //annual labor earnings
gen swages = 0
gen dividends = 0
gen intrec = 0
gen stcg = 0 
gen ltcg = 0
gen otherprop = 0
gen nonprop = 0     
gen pensions = 0    //pension distributions
gen gssi = 0        //SS benefits
gen ui = 0
gen transfers = 0
gen rentpaid = 0
gen proptax = 0
gen otheritem = 0
gen childcare = 0
gen mortgage = 0

replace pwages = v1
replace page = v3

drop v1 v3

*Run the tax calculator
cd "$indata"
taxsim35, replace

*Keep relevant vars
keep fica pwages 
rename pwages earn_before_dc
gen id = _n
merge 1:1 id using "$indata/workinc_s64_a_fiitax"
drop _merge id
order page earn_before_dc fica earn_after_dc fiitax dc_contrib

*Export as csv for input into structural model
export delimited "$indata/workinc_taxsim_s64_a_20.csv", novarnames replace

